home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / faq / vol15n14.zip / DDGAME.ZIP / GAME.CPP < prev    next >
C/C++ Source or Header  |  1996-04-17  |  4KB  |  144 lines

  1. // Game.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Game.h"
  6.  
  7. #include "GameFrame.h"
  8. #include "Animation.h"
  9.  
  10. #include <mmsystem.h>
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. //***************************************************************************
  19. // The one and only CGameApp object
  20. //***************************************************************************
  21.  
  22. //CGameApp theApp;
  23.  
  24. //-----------------------------------------------------------------------------
  25. // default constructor
  26. //-----------------------------------------------------------------------------
  27. CGameApp::CGameApp()
  28. {
  29.   m_bPlaying = FALSE;
  30.   m_displayWidth = 640;
  31.   m_displayHeight = 480;
  32.   CAnim::SetDisplayWidth(m_displayWidth);
  33. }
  34.  
  35. //-----------------------------------------------------------------------------
  36. // InitInstance
  37. //-----------------------------------------------------------------------------
  38. BOOL CGameApp::InitInstance()
  39. {
  40.     // Standard initialization
  41.  
  42. #ifdef _AFXDLL
  43.     Enable3dControls();            // Call this when using MFC in a shared DLL
  44. #else
  45.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  46. #endif
  47.  
  48.     m_pMainWnd = GetNewFrame();
  49.     ASSERT(m_pMainWnd != NULL);
  50.  
  51.     m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
  52.   ShowCursor(FALSE);
  53.  
  54.     return TRUE;
  55. }
  56.  
  57. //-----------------------------------------------------------------------------
  58. // virtual GetNewFrame
  59. //-----------------------------------------------------------------------------
  60. CWnd* CGameApp::GetNewFrame()
  61. {
  62.   return new CGameFrame();
  63. }
  64.  
  65.  
  66. //-----------------------------------------------------------------------------
  67. // GetGameFrame
  68. //-----------------------------------------------------------------------------
  69. CGameFrame* CGameApp::GetGameFrame()
  70. {
  71.   return (CGameFrame*)m_pMainWnd;
  72. }
  73.  
  74. //-----------------------------------------------------------------------------
  75. // virtual StartPlay
  76. //-----------------------------------------------------------------------------
  77. void CGameApp::StartPlay()
  78. {
  79.   m_bPlaying = TRUE;
  80. }
  81.  
  82. //-----------------------------------------------------------------------------
  83. // virtual StopPlay
  84. //-----------------------------------------------------------------------------
  85. void CGameApp::StopPlay()
  86. {
  87.   m_bPlaying = FALSE;
  88. }
  89.  
  90. //-----------------------------------------------------------------------------
  91. // virtual Run
  92. //-----------------------------------------------------------------------------
  93. int CGameApp::Run() 
  94. {
  95.     ASSERT_VALID(this);
  96.  
  97.   // Initialize the graphics
  98.   if (!GetGameFrame()->InitGraphics())
  99.   {
  100.     GetGameFrame()->KillGraphics();
  101.     return ExitInstance();
  102.   }
  103.  
  104.   // Give most of the processor time to the application (but not all!)
  105.   SetThreadPriority(THREAD_PRIORITY_HIGHEST);
  106.  
  107.     // acquire and dispatch messages until a WM_QUIT message is received.
  108.     for (;;)
  109.     {
  110.         if (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
  111.         {
  112.             // pump message, but quit on WM_QUIT
  113.             if (!PumpMessage())
  114.       {
  115.         GetGameFrame()->KillGraphics();
  116.         GetGameFrame()->DestroyWindow();
  117.  
  118.         return ExitInstance();
  119.       }
  120.         }
  121.  
  122.         if (m_bPlaying)
  123.         {
  124.             if (!GetGameFrame()->UpdateGame())
  125.       {
  126.         StopPlay();
  127.         PostQuitMessage(0);
  128.       }
  129.         }
  130.     else
  131.     {
  132.       ::WaitMessage();
  133.     }
  134.     }
  135.  
  136.     ASSERT(FALSE);  // not reachable
  137. }
  138.  
  139. BEGIN_MESSAGE_MAP(CGameApp, CWinApp)
  140.     //{{AFX_MSG_MAP(CGameApp)
  141.     //}}AFX_MSG_MAP
  142. END_MESSAGE_MAP()
  143.  
  144.